<template>
  <div class="shopping-car">
    <div class="goods-list">
      <!-- 商品列表区域 -->
      <div class="mui-card" v-for="(item, i) in goodslist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getGoodsSelect[item.id]"
              @change="selectChange(item.id, $store.getters.getGoodsSelect[item.id])"
            ></mt-switch>
            <img :src="item.pic" />
            <div class="info">
              <h1>{{item.title}}</h1>
              <p>
                <span class="price">￥{{item.price}}</span>
                <numbox :initCount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                <a href="#" @click.prevent="remove(item.id, i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计（不含运费）：</p>
            <p>
              已勾选商品
              <span class="red">{{ $store.getters.getCountAndAmount.count}}</span> 件，总价 ￥
              <span class="red">{{ $store.getters.getCountAndAmount.amount}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>

    <p>{{$store.getters.getGoodsSelect}}</p>
  </div>
</template>

<script>
import numbox from "../subcomponents/shoppingCar_numbox.vue";
export default {
  data() {
    return {
      goodslist: [],
      goodsall: []
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      this.goodslist = this.$store.state.car;

      //   // 获取到 store 中所有商品的 ID 和 number
      //   var itemArr = [];
      //   this.$store.state.car.forEach(item => {
      //     itemArr.push({ id: item.id, number: item.count });
      //   });
      //   if (itemArr.length <= 0) {
      //     return;
      //   }

      //   // 将商品加入购物车
      //   itemArr.forEach(item => {
      //     this.$http.post("https://api.it120.cc/lisa/shopping-cart/add", {
      //       goodsId: item.id,
      //       number: item.number
      //     });
      //   });

      //   //  获取购物车中的商品
      //   this.$http.get("https://api.it120.cc/lisa/shopping-cart/info").then(result => {
      //      if(result.body.code === 0){
      //         this.goodslist = result.body.data;
      //      }
      //   });
    },

    remove(id, index) {
      // 点击删除，把商品从 store 中根据 id 删除，同时，使用 index 把当前组件 goodslist 中对应要删除的商品删除
      this.goodslist.splice(index, 1);
      this.$store.commit("removeFromCar", id);
    },

    selectChange(id, val) {
      this.$store.commit("updateGoodsSelect", { id, selected: val });
    }
  },
  components: {
    numbox
  }
};
</script>

<style lang="scss" scoped>
.shopping-car {
  background-color: #ccc;
  overflow: hidden;
  .goods-list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
    }
    img {
      width: 60px;
      height: 60px;
      margin: 5px;
    }
    h1 {
      font-size: 13px;
      margin: 5px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .price {
        font-weight: bold;
        color: red;
      }
    }
  }

  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      font-size: 16px;
      font-weight: bold;
      color: red;
    }
  }
}
</style>